package com.frinder.base.akka;

import akka.actor.ActorRef;
import akka.event.japi.LookupEventBus;
import lombok.Builder;
import lombok.ToString;

/**
 * @ClassName MyLookupEventBus
 * @Description akka 消息订阅 bus
 * @Author frinder
 * @Date 2019/1/9 15:08
 * @Version 1.0
 */
public class MyLookupEventBus extends LookupEventBus<MyLookupEventBus.MsgEnvelope, ActorRef, MyLookupEventBus.Topic> {

    public static final MyLookupEventBus BUS = new MyLookupEventBus();

    public enum Topic {
        SYSTEM_MESSAGE_EVENT,
        MAP_MESSAGE_EVENT
    }

    @Override
    public int mapSize() {
        return 256;
    }

    @Override
    public int compareSubscribers(ActorRef a, ActorRef b) {
        return a.compareTo(b);
    }

    @Override
    public Topic classify(MsgEnvelope event) {
        return event.topic;
    }

    @Override
    public void publish(MsgEnvelope event) {
        super.publish(event);
    }

    @Override
    public void publish(MsgEnvelope event, ActorRef subscriber) {
        subscriber.tell(event.payload, ActorRef.noSender());
    }

    @Builder
    @ToString
    public static class MsgEnvelope {
        public Topic topic;
        public AbstractEvent payload;
    }
}
